package com.demoworld;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializeConfig;
import com.alibaba.fastjson.util.IdentityHashMap;
import com.demoworld.entity.Student;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.Gson;

import java.util.UUID;

/**
 * Created by francis on 2018/1/21.
 */
public class Main {
    public static void main(String[] args) throws JsonProcessingException {
//        jacksonTest();
//        gsonTest();
//        fastJsonTestFix();
        jacksonTestFix();
    }

    static void fastJsonTest(){
        Student student = new Student();
        student.setName("francis");
        student.setId(UUID.randomUUID().toString());
        System.out.println(JSON.toJSONString(student));
        System.out.println("fastjson over");
    }
    //修复问题的测试
    static void fastJsonTestFix(){
        Student student = new Student();
        student.setName("francis");
        student.setId(UUID.randomUUID().toString());
        SerializeConfig serializeConfig = new SerializeConfig(IdentityHashMap.DEFAULT_SIZE,  true);

        System.out.println(JSON.toJSONString(student, serializeConfig));
        System.out.println("fastjson over");
    }
    static void gsonTest() {
        Student student = new Student();
        student.setName("francis");
        student.setId(UUID.randomUUID().toString());

        Gson gson = new Gson();
        System.out.println(gson.toJson(student));

        System.out.println("gson over");
    }

    static void jacksonTest() throws JsonProcessingException {
        Student student = new Student();
        student.setName("francis");
        student.setId(UUID.randomUUID().toString());

        ObjectMapper objectMapper = new ObjectMapper();
        System.out.println(objectMapper.writeValueAsString(student));

        System.out.println("jackson over");
    }

    static void jacksonTestFix() throws JsonProcessingException {
        Student student = new Student();
        student.setName("francis");
        student.setId(UUID.randomUUID().toString());

        ObjectMapper objectMapper = new ObjectMapper();
        System.out.println(objectMapper.writeValueAsString(student));

        System.out.println("jackson over");
    }
}
